﻿@using HZY.Infrastructure;
@using FreeSql.DatabaseModel;
@using HZY.Models.Entities.LowCode
@model HZY.Models.DTO.DevelopmentTool.GenDbTableDto
@{
    var className = Model.EntityName;
    var classNameRemark = Model.DisplayName;

    var ignores = new string[] {
        "Id",
        "CreationTime",
        "CreatorUserId",
        "LastModificationTime",
        "LastModifierUserId" ,
        "DeletionTime",
        "DeleterUserId",
        "IsDeleted"
    };

    var tableInfos = Model.TableInfos
        .Where(w => !ignores.Contains(w.ColumnName))
        .OrderBy(w => w.Position)
        .ToList()
        ;
}

@functions
{
    //获取类型 根据 appTableInfo
    string GetType(DbColumnInfo appTableInfo)
    {
        switch (appTableInfo.DbTypeText)
        {
            case "uniqueidentifier":
                return appTableInfo.IsPrimary ? "Guid" : "Guid?";
            case "bit":
            case "int":
                return appTableInfo.IsPrimary ? "int" : "int?";
            case "datetime":
                return appTableInfo.IsNullable ? "DateTime?" : "DateTime";
            case "float":
            case "money":
                return appTableInfo.IsNullable ? "double?" : "double";
            case "decimal":
                return appTableInfo.IsNullable ? "decimal?" : "decimal";
            default:
                return appTableInfo.IsNullable ? appTableInfo.CsType.Name + "?" : appTableInfo.CsType.Name;
        }
    }
    //获取类型 根据 appTableInfo
    string GetTypeNew(LowCodeTableInfo appTableInfo)
    {
        if (appTableInfo.CsType.ToLower().Equals("string")) return appTableInfo.CsType;
        return appTableInfo.IsNullable ? $"{appTableInfo.CsType}?" : appTableInfo.CsType;
    }
}

<pre>
using System;
using System.ComponentModel.DataAnnotations;
using @(Model.Namespace).Models.Entities.BaseEntitys;

namespace @(Model.Namespace).Models.Entities
{
    /// <summary>
    /// @(string.IsNullOrWhiteSpace(classNameRemark) ? className : classNameRemark)
    /// </summary>
    public class @className : DefaultBaseEntity<@("Guid")>
    {
        @foreach (var item in tableInfos)
        {
            <pre>
            /// <summary>
            ///  @(string.IsNullOrWhiteSpace(item.DisplayName) ? item.ColumnName : item.DisplayName) => 备注: @(string.IsNullOrWhiteSpace(item.Describe) ? item.ColumnName: item.Describe)
            /// </summary>
            public @(GetTypeNew(item)) @item.ColumnName { get; set; }
            </pre>
        }

    }
}
</pre>